<?php
/**
* @package Pages-and-Items (com_pi_pages_and_items)
* @version 1.5.7
* @copyright Copyright (C) 2006-2010 Carsten Engel. All rights reserved.
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
* @author http://www.pages-and-items.com
*/

//no direct access
if(!defined('_VALID_MOS') && !defined('_JEXEC')){
	die('Restricted access');
}

if($class_pi->user_type!='Super Administrator'){
	echo "<script> alert('you need to be logged in as a super administrator to edit the Pages-and-Items config.'); window.history.go(-1); </script>";
	exit();
}

echo '<link rel="stylesheet" type="text/css" href="components/com_pi_pages_and_items/css/pages_and_items3.css" />';

//if Joomla 1.0.x do some style changes
if(!defined('_JEXEC')){
	//joomla 1.0.x
	echo '<link rel="stylesheet" type="text/css" href="components/com_pi_pages_and_items/css/joomla_1_0_x.css" />';
}

$type_id = intval($class_pi->get_var('type_id', ''));

//get url together for after rendering
$futuretask = $class_pi->get_var('futuretask', '');
$url_after_processing = 'index2.php?option=com_pi_pages_and_items&task=';
if($futuretask=='config'){
	$url_after_processing .= 'config&tab=itemtypes';
}elseif($futuretask=='config_custom_itemtype'){
	$url_after_processing .= 'config_custom_itemtype&type_id='.$type_id;
}elseif($futuretask=='config_custom_itemtype_field'){
	$field_id = $class_pi->get_var('field_id', '');
	$url_after_processing .= 'config_custom_itemtype_field&field_id='.$field_id;
}	

echo '<div style="width: 600px; margin: 0 auto;">';
echo '<p>';	
//check what message to display
$from = $class_pi->get_var('from', '');
if(($futuretask=='config' || $futuretask=='config_custom_itemtype') && $from!='field'){
	echo _pi_lang_custom_itemtype_saved;
	$message_after_rendering = _pi_lang_custom_itemtype_saved;
}else{
	echo _pi_lang_field_saved;
	$message_after_rendering = _pi_lang_field_saved;	
}



echo '</p>';

echo '<h1>';
echo 'updating items of this itemtype...';
echo ' <img src="components/com_pi_pages_and_items/images/processing.gif" alt="updating items of this itemtype" />';
echo '</h1>';



$itemtype_name = 'custom_'.$type_id;

//get items which need updating			
$class_pi->db->setQuery( "SELECT c.id, c.title, c.catid, c.sectionid"
. "\nFROM #__content AS c"		
. "\nLEFT JOIN #__pi_item_index AS i"
. "\nON c.id=i.item_id"
. "\nWHERE i.itemtype='$itemtype_name'"
. "\nAND (c.state='0' OR c.state='1') "
. "\nORDER BY c.title ASC"
);		
$items_array = $class_pi->db->loadObjectList();

$total_to_render = count($items_array);
echo '<p>';
echo 'total items to render = '.$total_to_render;
echo '</p>';

if($total_to_render==0){
	$message_after_rendering .=  '. '._pi_lang_no_items_to_update.'.';
	$class_pi->redirect_to_url($url_after_processing, $message_after_rendering);
}

//make javascript array of item id's
$javascript_array_items = 'var pi_array_items = new Array(';
$first = true;						
foreach($items_array as $item){		
	if($first){
		$first = false;
	}else{
		$javascript_array_items .= ',';
	}
	$javascript_array_items .= "'".$item->id."'";
}
$javascript_array_items .= ');';

//make sure mootools is loaded

if(defined('_JEXEC')){
	//joomla 1.5
	JHTML::_('behavior.mootools');
}else{
	//joomla 1.0.x
	echo '<script language="JavaScript" type="text/javascript" src="components/com_pi_pages_and_items/javascript/mootools.js"></script>';
}


?>

<script language="javascript" type="text/javascript">

<?php echo $javascript_array_items."\n"; ?>


var custom_itemtype_id = '<?php echo $type_id; ?>';
var ajax_url = 'index2.php?option=com_pi_pages_and_items&task=ajax_update_cit_item&format=raw';

/*
window.addEvent('domready', function() {
	for (i = 0; i < pi_array_items.length; i++){
		ajax_url = 'index2.php?option=com_pi_pages_and_items&task=ajax_update_cit_item&format=raw&itemtype='+custom_itemtype_id+'&item_id='+pi_array_items[i];		
		req=new Ajax(ajax_url,           
		{
		update:'item_'+pi_array_items[i],
		//method:'post'
		onComplete:progress_bar                   
		}
		);	   
		req.request();
	}
});
*/

window.addEvent('domready', function() {
	var delay = 0;
	for (i = 0; i < pi_array_items.length; i++){  
		ajax_url = 'index2.php?option=com_pi_pages_and_items&task=ajax_update_cit_item&format=raw&itemtype='+custom_itemtype_id+'&item_id='+pi_array_items[i];	   
		var req = new Ajax(ajax_url,{ update:'item_'+pi_array_items[i], onComplete:progress_bar });
		delay += 500; // 0.5 seconds between each call
		req.request.delay(delay,req);          
	}
});

var rendered = 0;
var total_to_render = '<?php echo $total_to_render; ?>';

var percent;

function progress_bar(){
	rendered = rendered+1;	
	ready = total_to_render/rendered;		
	percent = 100/ready;	
	percent = Math.floor(percent);	
	document.getElementById('percent').innerHTML = percent+'%';	
	progress_width = percent*4;		
	document.getElementById('progress').style.width = progress_width+'px';
	if(ready==1){	
		//alert('ready');
		document.location.href = '<?php echo $url_after_processing; ?>';
	}
}

</script>
<div id="percent">
0%
</div>
<div style="width: 400px; height: 20px; border: 1px solid #ccc">
	<div id="progress" style="background: #000033; height: 20px; width: 0px;">
		&nbsp;
	</div>
</div>
<?php

echo '<table cellpadding="5">';
echo '<tr>';
echo '<th>';
echo 'id';
echo '</th>';
echo '<th>';
echo 'title';
echo '</th>';
echo '<th>';
echo 'status';
echo '</th>';
echo '<th>';
echo 'category id';
echo '</th>';
echo '<th>';
echo 'section id';
echo '</th>';
echo '</tr>';
foreach($items_array as $item){
	//$this->update_content_table_from_custom_itemtype($item_id, $itemtype_name);
	//check if the saved item has other_items linked to it, if so, update those			
	//$this->update_other_items_if_needed($item_id);
	echo '<tr>';
	echo '<td>';
	echo $item->id;
	echo '</td>';
	echo '<td>';
	echo $item->title;
	echo '</td>';
	echo '<td id="item_'.$item->id.'">&nbsp;';	
	echo '</td>';
	echo '<td>';
	echo $item->catid;
	echo '</td>';
	echo '<td>';
	echo $item->sectionid;
	echo '</td>';
	echo '</tr>';
}	
echo '</table>';
echo '<div>';

?>